package cn.jane.rpc.netty.transport.command;

import java.nio.charset.StandardCharsets;

/**
 * 响应的头部
 */
public class ResponseHeader extends Header {

    private int code;
    private String error;

    public ResponseHeader(int type, int version, int requestId) {
        this(type, version, requestId, ResponseCode.SUCCESS.getCode(), null);
    }

    public ResponseHeader(int type, int version, int requestId, String error) {
        this(type, version, requestId, ResponseCode.UNKNOWN_ERROR.getCode(), error);
    }

    public ResponseHeader(int requestId, int version, int type, int code, String error) {
        super(requestId, version, type);
        this.code = code;
        this.error = error;
    }

    @Override
    public int length() {
        return super.length() +
               // code 的长度
               Integer.BYTES +
               // len 的长度 TODO ?
               Integer.BYTES +
               // error 的长度
               (error == null ? 0 : error.getBytes(StandardCharsets.UTF_8).length);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
}
